package com.aode.downloadmusic.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.aode.common.bean.response.Music;
import com.aode.downloadmusic.R;
import com.aode.downloadmusic.databinding.ItemDownloadMusicBinding;
import com.aode.utils.bitmap.BitmapUtils;

import java.util.ArrayList;

public class DownloadMusicAdapter extends RecyclerView.Adapter{
    private MusicInterface musicInterface;
    private Context context;
    private ArrayList<Music> musicArrayList;
    public DownloadMusicAdapter(Context context, ArrayList<Music> musicArrayList, MusicInterface musicInterface){
        this.context = context;
        this.musicArrayList = musicArrayList;
        this.musicInterface = musicInterface;
    }
    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemDownloadMusicBinding downloadMusicBinding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.item_download_music,parent,false);
        return new DownloadMusicHolder(downloadMusicBinding);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if(holder instanceof DownloadMusicHolder){
            DownloadMusicHolder downloadMusicHolder = (DownloadMusicHolder) holder;
            if(musicArrayList!=null&&!musicArrayList.isEmpty()&&musicArrayList.size()>0){
                Music music = musicArrayList.get(position);
                String url = music.getMusicCover();
                if(url!=null&&!url.equals("")){
                    Bitmap bitmap = BitmapFactory.decodeFile(url);
                    downloadMusicHolder.getDownloadMusicBinding().ivMusic.setImageBitmap(bitmap);
                }

                downloadMusicHolder.getDownloadMusicBinding().setMusic(music);
                downloadMusicHolder.getDownloadMusicBinding().setMusicInterface(musicInterface);
            }
        }
    }

    @Override
    public int getItemCount() {
        return musicArrayList.size();
    }
    class DownloadMusicHolder extends RecyclerView.ViewHolder{
        private ItemDownloadMusicBinding downloadMusicBinding;
        public DownloadMusicHolder(ItemDownloadMusicBinding downloadMusicBinding) {
            super(downloadMusicBinding.getRoot());
            this.downloadMusicBinding = downloadMusicBinding;
        }

        public ItemDownloadMusicBinding getDownloadMusicBinding() {
            return downloadMusicBinding;
        }

        public void setDownloadMusicBinding(ItemDownloadMusicBinding downloadMusicBinding) {
            this.downloadMusicBinding = downloadMusicBinding;
        }
    }
    public interface MusicInterface{
        void onItemClick(Music music);
    }
}
